#include <bits/stdc++.h>

#define eb emplace_back
#define ep emplace
#define fi first
#define se second
#define in read<int>()
#define rep(i, x, y) for(int i = (x); i <= (y); i++)
#define per(i, x, y) for(int i = (x); i >= (y); i--)

using namespace std;

using ll = long long;
using pii = pair < int, int >;
using vec = vector < int >;
using veg = vector < pii >;

template < typename T > T read() {
  T x = 0; bool f = 0; char ch = getchar();
  while(!isdigit(ch)) f |= ch == '-', ch = getchar();
  while(isdigit(ch)) x = x * 10 + (ch ^ 48), ch = getchar();
  return f ? -x : x;
}

template < typename T > void chkmax(T &x, const T &y) { x = x > y ? x : y; }
template < typename T > void chkmin(T &x, const T &y) { x = x < y ? x : y; }

const int N = 1e6 + 10;

int n, m;
int siz[N], son[N], cnt[N], ans[N], rt, psz, bsz, bcnt[N];
bool vis[N];

vec G[N];
veg qry[N];

void gsz(int x, int p) {
  son[x] = 0; siz[x] = 1; for(auto y : G[x]) if(!vis[y] && y ^ p) gsz(y, x), siz[x] += siz[y], chkmax(son[x], siz[y]);
}

void grt(int x, int p, int tsiz) {
  for(auto y : G[x]) if(!vis[y] && y ^ p) grt(y, x, tsiz); chkmax(son[x], tsiz - siz[x]); if(rt == 0 || son[x] < son[rt]) rt = x;
}

void dfs(int x, int p, int dis, int *cnt, int &psz) {
  cnt[dis]++; chkmax(psz, dis);
  for(auto y : G[x])
    if(!vis[y] && y ^ p) dfs(y, x, dis + 1, cnt, psz);
}

int calc1(int d) { return d < 0 || d > psz ? 0 : cnt[d]; }
int calc2(int d) { return d < 0 || d > bsz ? 0 : bcnt[d]; }

void tdfs(int x, int p, int dis) {
  for(auto y : G[x])
    if(!vis[y] && y ^ p) {
      tdfs(y, x, dis + 1);
    }
  for(auto v : qry[x]) {
    int d, id; tie(d, id) = v;
    ans[id] += calc1(d - dis) - calc2(d - dis);
  }
}

void divide(int x) {
  gsz(x, 0); rt = 0; grt(x, 0, siz[x]); x = rt; vis[x] = true; //cerr << x << endl;
  psz = 0; dfs(x, 0, 0, cnt, psz); //rep(i, 1, psz) cnt[i] += cnt[i - 1];
  for(auto v : qry[x]) {
    int d, id; tie(d, id) = v;
    ans[id] += calc1(d);
  }
  for(auto y : G[x])
    if(!vis[y]) {
      bsz = 0; dfs(y, x, 1, bcnt, bsz); //rep(i, 1, bsz) bcnt[i] += bcnt[i - 1];
      tdfs(y, x, 1); rep(i, 0, bsz) bcnt[i] = 0;
    }
  rep(i, 0, psz) cnt[i] = 0;
  for(auto y : G[x]) if(!vis[y]) divide(y);
}

void solve() {
  n = in, m = in;
  rep(i, 1, n) qry[i].clear(), G[i].clear(), vis[i] = 0;
  rep(i, 1, n - 1) {
    int u = in, v = in;
    G[u].eb(v); G[v].eb(u);
  }
  rep(i, 1, m) {
    ans[i] = 0; int u = in, p = in;
    qry[u].eb(p, i);
  }
  divide(1);
  rep(i, 1, m) printf("%d\n", ans[i]);
}

int main() {
#ifndef ONLINE_JUDGE
  freopen("1.in", "r", stdin);
#endif
  for(int T = in; T; T--) solve();
  return 0;
}
